package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.db.DataManager;
import com.example.myapplication.db.domain.AccountBean;
import com.example.myapplication.db.DBManager;
import com.example.myapplication.db.DBOpenHelper;
import com.example.myapplication.db.DBUtils;
import com.example.myapplication.db.NetworkUtils;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static DBOpenHelper db;
    //
    private ListView listView;

    List<AccountBean> itemList;
    List<AccountBean> yearList;

    private TextView year,month;
    private int year1,month1;
    int user_id;

    private TextView out_money,in_money;

//     选择时间
    private Spinner year_spinner,month_spinner;

    //   底部导航栏
    private ImageView main_bottom_01,
            main_bottom_02,
            main_bottom_03,
            main_bottom_04,
            main_bottom_05;


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(DataManager.isLogin){
             user_id = DataManager.userId;
        }

        // 获取listview
        listView = findViewById(R.id.show_details);
        //我添加不了下面这几行，如果加了就会闪退出去。但是要取得表头的年月，现在还没取得。
        year=findViewById(R.id.main_top_year);
        month=findViewById(R.id.main_top_month);
        year_spinner = findViewById(R.id.main_top_spinner1);
        month_spinner = findViewById(R.id.main_top_spinner2);
        out_money=findViewById(R.id.item_mainlv_top_out_money);
        in_money=findViewById(R.id.item_mainlv_top_in_money);
        //        绑定底部导航栏
        main_bottom_01 = findViewById(R.id.main_bottom_01);
        main_bottom_02 = findViewById(R.id.main_bottom_02);
        main_bottom_03 = findViewById(R.id.main_bottom_03);
        main_bottom_04 = findViewById(R.id.main_bottom_04);
        main_bottom_05 = findViewById(R.id.main_bottom_05);

//      导航底部绑定跳转事件
        main_bottom_02.setOnClickListener(view -> {
            Intent intent = new Intent(this, ChartOutcomeActivity.class);
            startActivity(intent);
        });
        main_bottom_03.setOnClickListener(view -> {
            Intent intent = new Intent(this, addFragment.class);
            startActivity(intent);
        });
        main_bottom_04.setOnClickListener(view -> {
            Intent intent = new Intent(this, FindActivity.class);
            startActivity(intent);
        });
        main_bottom_05.setOnClickListener(view -> {
            Intent intent = new Intent(this, personalPage.class);
            startActivity(intent);
        });


        year_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                year.setText(year_spinner.getSelectedItem().toString());
                year1=Integer.parseInt(year.getText().toString().split("年")[0]);
                month1=Integer.parseInt(month.getText().toString());
                System.out.println(year1 + "年" + month1 +"月");

                yearList = DBManager.findAccountsByYear(user_id, year1);
                if (yearList==null||yearList.size()==0){
                    out_money.setText("0.00");
                    in_money.setText("0.00");
                }else {
                    itemList = getListByMonth(yearList, month1);
                }

                System.out.println(itemList);

                //  定义适配器
                if(itemList!=null){
                    ItemAdapter adapter=new ItemAdapter(MainActivity.this,R.layout.account_item,itemList);
                    listView.setAdapter(adapter);
                }

                if (itemList==null||itemList.size()==0){
                    out_money.setText("0.00");
                    in_money.setText("0.00");
                }else{
                    out_money.setText(""+getOutMoney(itemList));
                    in_money.setText(""+getInMoney(itemList));
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        month_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                month.setText(month_spinner.getSelectedItem().toString().split("月")[0]);
                month1=Integer.parseInt(month.getText().toString());
                System.out.println(year1 + "年" + month1 +"月");

                if (yearList==null||yearList.size()==0){
                    out_money.setText("0.00");
                    in_money.setText("0.00");
                }else {
                    itemList= getListByMonth(yearList, month1);
                }

                System.out.println(itemList);

                //  定义适配器
                if(itemList!=null){
                    ItemAdapter adapter=new ItemAdapter(MainActivity.this,R.layout.account_item,itemList);
                    listView.setAdapter(adapter);
                }

                if (itemList==null||itemList.size()==0){
                    out_money.setText("0.00");
                    in_money.setText("0.00");
                }else{
                    out_money.setText(""+getOutMoney(itemList));
                    in_money.setText(""+getInMoney(itemList));
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        //  TODO:准备数据
        if(DataManager.isLogin){
             user_id = DataManager.userId;
        }
        System.out.println(year1);
        yearList = DBManager.findAccountsByYear(user_id, year1);
        if (yearList!=null){
            itemList = getListByMonth(yearList, month1);
        }
        System.out.println(user_id);
        System.out.println(itemList);
        //  定义适配器
        if(itemList!=null){
            ItemAdapter adapter=new ItemAdapter(MainActivity.this,R.layout.account_item,itemList);
            listView.setAdapter(adapter);
        }

        if (itemList == null || itemList.size() == 0) {
            out_money.setText("0.00");
            in_money.setText("0.00");
        }

//        默认选中的年份
        year_spinner.setSelection(3);
//        默认选中的月份
//        获取当前月份
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        month_spinner.setSelection(month-1);


    }

    private List<AccountBean> getListByMonth(List<AccountBean> yearList,int month) {
        List<AccountBean> monthList = new ArrayList<>();
        for (AccountBean item : yearList) {
            if (item.getMonth() == month) {
                monthList.add(item);
            }
        }
        return monthList;
    }

    private float getOutMoney(List<AccountBean> itemList) {
        float outMoney = 0;
        for (AccountBean item : itemList) {
            if (item.getIn_out().equals("支出")) {
                outMoney += item.getMoney();
            }
        }
        return outMoney;
    }

    private float getInMoney(List<AccountBean> itemList) {
        float inMoney = 0;
        for (AccountBean item : itemList) {
            if (item.getIn_out().equals("收入")) {
                inMoney += item.getMoney();
            }
        }
        return inMoney;
    }

    //    点击事件
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_mainlv_lv:
//                点击后跳转到记账页面（选择类别）
                break;
            case R.id.item_mainlv_top_out_money:
//                点击支出查看该月支出详情
                break;
            case R.id.item_mainlv_top_in_money:
//                点击收入查看该月收入详情
                break;
        }
    }



    //初始化自带的view方法
    private void initView() {
        setLVLongClickListener();
    }


    private void setLVLongClickListener() {
    //    设置账单记录的长按事件
    }

    public void jumpToSearch(View view) {
//        点击搜索图标，进行搜索操作
    }

}
